package lex.title.mac;

import java.awt.Container;
import java.awt.Dimension;
import lex.title.basic.BasicTitleLayout;

class MacTitleLayout extends BasicTitleLayout {
	private final MacWindowTitle title;

	MacTitleLayout(MacWindowTitle title) {
		super(title);
		this.title = title;
		this.setTitleExtraSpace(5);
	}

	public void layoutContainer(Container c) {
		if (title.isNoneDecoration())
			return;
		if (true == title.isLeftToRight())
			this.layoutLeftToRight(c);
		else
			this.layoutRightToLeft(c);
	}

	/*
	 * (close)(min)(max-restore)-----...-----(menu)
	 */
	public void layoutLeftToRight(Container c) {
		int w = title.getWidth();
		int h = title.getHeight();
		Dimension buttonSize = title.getCloseButton().getPreferredSize();
		// layout close button
		int x = getEdgeGap();
		int y = (h - buttonSize.height) / 2;
		title.getCloseButton().setLocation(x, y);
		// layout minimize button
		x += buttonSize.width + getButtonGap();
		title.getMinimizeButton().setLocation(x, y);
		// layout max-restore button
		x += buttonSize.width + getButtonGap();
		title.getMaximizeRestoreButton().setLocation(x, y);
		// layout menu button
		buttonSize = title.getMenuButton().getPreferredSize();
		x = w - getEdgeGap() - buttonSize.width;
		y = (h - buttonSize.height) / 2;
		title.getMenuButton().setLocation(x, y);
	}

	/*
	 * (menu)-----...-----(max-restore)(min)(close)
	 */
	public void layoutRightToLeft(Container c) {
		int w = title.getWidth();
		int h = title.getHeight();
		Dimension buttonSize = null;
		// layout menu button
		buttonSize = title.getMenuButton().getPreferredSize();
		int x = getEdgeGap();
		int y = (h - buttonSize.height) / 2;
		title.getMenuButton().setLocation(x, y);
		// layout close button
		buttonSize = title.getCloseButton().getPreferredSize();
		x = w - getEdgeGap() - buttonSize.width;
		y = (h - buttonSize.height) / 2;
		title.getCloseButton().setLocation(x, y);
		// layout minimize button
		buttonSize = title.getMinimizeButton().getPreferredSize();
		x -= buttonSize.width + getButtonGap();
		title.getMinimizeButton().setLocation(x, y);
		// layout max-restore button
		buttonSize = title.getMaximizeRestoreButton().getPreferredSize();
		x -= buttonSize.width + getButtonGap();
		title.getMaximizeRestoreButton().setLocation(x, y);
	}
}
